package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findWithMaxScore(Long toUserId) {
        // 设置查询条件
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria)
                .limit(1)
                .with(Sort.by(Sort.Order.desc("score")));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }


    //根据被推荐人id，查询出具有好感的用户
    @Override
    public List<RecommendUser> findRecommendUserList(Long toUserId, Integer page, Integer pageSize) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(pageSize)
                .skip((page - 1) * pageSize);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        return list;
    }

    //根据userId和ToUserId查询推荐信息
    @Override
    public RecommendUser queryByUserId(Long userId, Long friendId) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("toUserId").is(friendId);
        Query query = Query.query(criteria);
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        return user;
    }
}
